<template>
  <div>
    <h1>类型列表</h1>
    <div>
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="类型名称">
          <el-input
            v-model="form.keyWord"
            placeholder="请输入类型名"
            clearable
          />
        </el-form-item>

        <el-form-item label="分类状态">
          <el-select
            v-model="form.typeId"
            placeholder="请选择分类状态"
            clearable
          >
            <el-option label="显示" value="1" />
            <el-option label="隐藏" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getType()">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" @click="showType">添加类型</el-button>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="typeId" label="ID" sortable />
      <el-table-column prop="typeName" label="名称" sortable />
      <el-table-column prop="typeTop" label="排序" sortable />
      <el-table-column prop="typeImage" label="图片" sortable>
        <template #default="scope">
          <el-image
            v-if="scope.row.typeImage != null"
            style="width: 80px; height: 80px"
            :src="scope.row.typeImage"
          />
        </template>
      </el-table-column>
      <el-table-column prop="typeStatus" label="状态" sortable>
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.typeStatus == 1">显示</el-tag>
          <el-tag type="danger" v-if="scope.row.typeStatus == 0">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" sortable>
        <template #default="scope">
          <el-button type="primary" size="small" @click="onadd(scope.row)"
            >添加</el-button
          >
          <el-button type="primary" size="small" @click="getType(scope.row)"
            >修改</el-button
          >
          <el-button type="primary" size="small" @click="getType(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" title="添加类型" width="500">
      <el-form
        :model="typeData"
        ref="ruleFormRef"
        style="max-width: 600px"
        :rules="rules"
      >
        <el-form-item
          label="分类名称"
          :label-width="formLabelWidth"
          prop="typeName"
        >
          <el-input v-model="typeData.typeName" autocomplete="off" />
        </el-form-item>

        <el-form-item
          label="分类图片"
          :label-width="formLabelWidth"
          prop="typeImage"
        >
          <el-upload
            class="avatar-uploader"
            :headers="headers"
            action="http://127.0.0.1:10010/product/type/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth" prop="typeTop">
          <el-input-number v-model="typeData.typeTop" :min="1" />
        </el-form-item>

        <el-form-item
          label="状态"
          :label-width="formLabelWidth"
          prop="typeStatus"
        >
          <el-radio-group v-model="typeData.typeStatus">
            <el-radio value="1" size="large">显示</el-radio>
            <el-radio value="0" size="large">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            添加
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { getTypeList, addType } from "../api/api";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
const tableData = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const imageUrl = ref("");
const ruleFormRef = ref<FormInstance>();
const form = ref({
  keyWord: "",
  typeId: null,
});
const typeData = ref({
  typeId: null,
  typeName: "",
  typeImage: null,
  typeTop: 0,
  typeStatus: "",
  typePid: 0,
});
const onadd = (row: any) => {
  dialogFormVisible.value = true;
  typeData.value = {
    typeId: null,
    typeName: "",
    typeImage: null,
    typeTop: 0,
    typeStatus: "",
    typePid: row.id,
  };
  imageUrl.value = "";
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      typeData.value.typeImage = imageUrl.value;
      addType(typeData.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          getType();
        } else {
          ElMessage.success(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
const checkAge = (rule: any, value: any, callback: any) => {
  if (imageUrl.value == "") {
    return callback(new Error("请选择图片"));
  } else {
    callback();
  }
};
const rules = ref<FormRules>({
  typeName: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
  typeTop: [
    {
      required: true,
      message: "请进行排序",
      trigger: "change",
    },
  ],
  typeStatus: [
    {
      required: true,
      message: "请选择类型状态",
      trigger: "change",
    },
  ],
  typeImage: [{ validator: checkAge, trigger: "blur" }],
});

const headers = ref({
  token: sessionStorage.getItem("token"),
});
const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
};
const showType = () => {
  dialogFormVisible.value = true;
  typeData.value = {
    typeId: null,
    typeName: "",
    typeImage: null,
    typeTop: 0,
    typeStatus: "",
    typePid: 0,
  };
  imageUrl.value = "";
};
const getType = () => {
  getTypeList(form.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data;
    }
  });
};
onMounted(() => {
  getType();
});
</script>
<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
